package com.hxzy.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.controller.vue.product.dto.DishFlavorItem;
import com.hxzy.controller.vue.product.dto.DishFlavorProperty;
import com.hxzy.entity.DishFlavor;
import com.hxzy.service.DishFlavorService;
import com.hxzy.mapper.DishFlavorMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 11452
* @description 针对表【dish_flavor(菜品口味关系表)】的数据库操作Service实现
* @createDate 2023-08-28 11:54:55
*/
@Service
public class DishFlavorServiceImpl extends ServiceImpl<DishFlavorMapper, DishFlavor>
    implements DishFlavorService{

    /**
     * 根据商品ID查询口味列表
     * @param productId
     * @return
     */
    @Override
    public List<DishFlavorProperty> dishFlavorsByPrdouctId(Long productId) {
        LambdaQueryWrapper<DishFlavor>  queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,productId);
        queryWrapper.eq(DishFlavor::getIsDeleted,"0");

        List<DishFlavor> dishFlavorList = this.list(queryWrapper);
        //转换成自己的自定义对象
        List<DishFlavorProperty>  newList=new ArrayList<>();
        if(!dishFlavorList.isEmpty()){
            for(DishFlavor  dishFlavor : dishFlavorList){
                DishFlavorProperty  dishFlavorProperty=new DishFlavorProperty();
                dishFlavorProperty.setName(dishFlavor.getName());
                //数据库 [{"value":"常规","price":0},{"value":"原创0糖0卡糖","price":1}] 转换成java List<DishFlavorItem>
                //使用hutool转换
                List<DishFlavorItem> dishFlavorItems = JSONUtil.toList(dishFlavor.getValue(), DishFlavorItem.class);
                dishFlavorProperty.setValue(dishFlavorItems);

                //放到集合中
                newList.add(dishFlavorProperty);
            }
        }
        return newList;
    }
}




